package {
	import org.flixel.*;
	public class Player extends FlxSprite {
		
		protected static const PLAYER_RUN_SPEED:int = 150;
		protected var direction:int = 0;
		public var element:int;
		protected var modeImg:Class;
		protected var isRunning:Boolean;
		protected var theGraphic:FlxSprite;
		
		public function Player(PLAYER_START_X:int, PLAYER_START_Y:int, playerGraphic:FlxSprite)
		{
			
			element = Elemental.NORMAL;
			modeImg = Res.PlayerImage;
			theGraphic = playerGraphic;
			super(PLAYER_START_X, PLAYER_START_Y);
			this.loadGraphic(Res.PlayerBoundingBox,false,false,20,20,false);
			theGraphic.loadGraphic(Res.PlayerImage,true,false,35,35,false);
			theGraphic.addAnimation("elementalRun", [0, 1, 2, 3], 5);
			theGraphic.addAnimation("playerRun", [0, 1], 10);
			theGraphic.addAnimation("playerIdle", [0]);
			theGraphic.play("playerIdle");
			isRunning = false;
		}
		
		public function theDirection():int {
			return direction;
		}
		
		public override function update():void 
		{ 
			if (Elemental.FREEZE) {
				velocity.x = 0;
				velocity.y = 0;
			} else {
				super.update();
				velocity.y = 0;
				velocity.x = 0;
				if(FlxG.keys.A) {
					if (FlxG.keys.W) {
						velocity.x = -PLAYER_RUN_SPEED * Math.cos(3.14159/4);
						velocity.y = -PLAYER_RUN_SPEED * Math.sin(3.14159/4);
						direction = 4;
					} else if (FlxG.keys.S) {
						velocity.x = -PLAYER_RUN_SPEED * Math.cos(3.14159/4);
						velocity.y = PLAYER_RUN_SPEED * Math.sin(3.14159/4);
						direction = 5;
					} else if (FlxG.keys.D) {
						velocity.x = 0;
						velocity.y = 0;
					} else {
						velocity.x = -PLAYER_RUN_SPEED;
						direction = 2;
					}
				} else if (FlxG.keys.D) {
					if (FlxG.keys.W) {
						velocity.x = PLAYER_RUN_SPEED * Math.cos(3.14159/4);
						velocity.y = -PLAYER_RUN_SPEED * Math.sin(3.14159/4);
						direction = 6;
					} else if (FlxG.keys.S) {
						velocity.x = PLAYER_RUN_SPEED  * Math.cos(3.14159/4);
						velocity.y = PLAYER_RUN_SPEED * Math.sin(3.14159/4);
						direction = 7;
					} else {
						velocity.x = PLAYER_RUN_SPEED;
						direction = 3;
					}
				} else if (FlxG.keys.W) {
					if (!FlxG.keys.S) {
						velocity.y = -PLAYER_RUN_SPEED;
						direction = 0;
					}
				} else if (FlxG.keys.S) {
					velocity.y = PLAYER_RUN_SPEED;
					direction = 1;
				}
			}
			
			if (element == Elemental.NORMAL) {
				if (isIdle()) {
					theGraphic.play("playerIdle");
					isRunning = false;
				} else if (!isRunning) {
					theGraphic.play("playerRun");
					isRunning = true;
				}
			}
			theGraphic.x = this.x - 8;
			theGraphic.y = this.y - 8;
		}
		
		public function isIdle():Boolean {
			return velocity.x == 0 && velocity.y == 0;
		}
		
		public function transform(mode:int):void {
			if (mode != element) {
				element = mode;
				switch(mode) {
					case Elemental.WATER:
						modeImg = Res.WaterPlayer;
						break;
					case Elemental.LIGHT:
						modeImg = Res.LightPlayer;
						break;
					case Elemental.WIND:
						modeImg = Res.WindPlayer;
						break;
					case Elemental.EARTH:
						modeImg = Res.EarthPlayer;
						break;
					case Elemental.MUD:
						modeImg = Res.MudPlayer;
						break;
					case Elemental.SAND:
						modeImg = Res.SandPlayer;
						break;
					case Elemental.ICE:
						modeImg = Res.IcePlayer;
						break;
					case Elemental.RAINBOW:
						modeImg = Res.RainbowPlayer;
						break;
					case Elemental.LIGHTNING:
						modeImg = Res.LightningPlayer;
						break;
					case Elemental.METAL:
						modeImg = Res.MetalPlayer;
						break;
					case Elemental.FIRE:
						modeImg = Res.FirePlayer;
						break;
					case Elemental.STEAM:
						modeImg = Res.SteamPlayer;
						break;
					case Elemental.WOOD:
						modeImg = Res.WoodPlayer;
						break;
					case Elemental.GLASS:
						modeImg = Res.GlassPlayer;
						break;
					case Elemental.VOID:
						modeImg = Res.VoidPlayer;
						break;
					case Elemental.NORMAL:
					default:
						modeImg = Res.PlayerImage;
						break;
				}
				theGraphic.loadGraphic(modeImg, true);
				theGraphic.play("elementalRun");
				if (!Music.MUTE){
					FlxG.play(Res.tranSound);
				}
			}
		}
	}
}